<?php

namespace App\Library;

use DateTime;
use Illuminate\Database\Events\QueryExecuted;

class QueryListener
{
    /**
     * Handle the event.
     *
     * @param QueryExecuted $event
     * @return void
     */
    public function handle(QueryExecuted $event): void
    {
        MySeasLog::debug([
            'type' => 'QueryListener',
            'sql' => strtr(vsprintf(strtr($event->sql, [
                '?' => "'%s'", '%' => '!~~~~~~~~!'
            ]), array_map(function ($binding) {
                if ($binding instanceof DateTime) return $binding->format('Y-m-d H:i:s');
                else return $binding;
            }, $event->bindings)), ['!~~~~~~~~!' => '%', '\\' => '']),
            'time' => $event->time . 'ms',
        ]);
    }
}
